<template>
  <div id="app">
    <div id="chart-container" ref="chartContainer" style="height: 100%; width: 100%; position: absolute; top: 10%; left: 0;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';

export default {
  name: 'EchartsDemo',
  mounted() {
    this.initChart();
    window.addEventListener('resize', this.resizeChart);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart);
  },
  methods: {
    initChart() {
      const chartContainer = this.$refs.chartContainer;
      this.myChart = echarts.init(chartContainer, null, {
        renderer: 'canvas',
        useDirtyRect: false
      });

      const posList = [
        'left', 'right', 'top', 'bottom', 'inside', 'insideTop', 'insideLeft', 'insideRight',
        'insideBottom', 'insideTopLeft', 'insideTopRight', 'insideBottomLeft', 'insideBottomRight'
      ];

      const app = this.app = {
        configParameters: {
          rotate: { min: -90, max: 90 },
          align: { options: { left: 'left', center: 'center', right: 'right' } },
          verticalAlign: { options: { top: 'top', middle: 'middle', bottom: 'bottom' } },
          position: { options: posList.reduce((map, pos) => ({ ...map, [pos]: pos }), {}) },
          distance: { min: 0, max: 100 }
        },
        config: {
          rotate: 90,
          align: 'left',
          verticalAlign: 'middle',
          position: 'insideBottom',
          distance: 15,
          onChange: () => {
            const labelOption = {
              rotate: app.config.rotate,
              align: app.config.align,
              verticalAlign: app.config.verticalAlign,
              position: app.config.position,
              distance: app.config.distance
            };
            this.myChart.setOption({
              series: Array(4).fill({ label: labelOption })
            });
          }
        }
      };

      const labelOption = {
        show: true,
        position: app.config.position,
        distance: app.config.distance,
        align: app.config.align,
        verticalAlign: app.config.verticalAlign,
        rotate: app.config.rotate,
        // 柱状图表内名字
        // formatter: '{c}  {name|{a}}',
        fontSize: 0,
        rich: { name: {} }
      };

      const option = {
        // tooltip鼠标经过显示
        tooltip: {
          trigger: 'axis',
          confine: true,          // 强制提示框限制在图表区域内
          position: 'right',      // 优先右侧显示（根据实际情况调整）
          axisPointer: { type: 'shadow' }
        },
        legend: { data: ['国内先进', '国内领先', '国际先进', '国际领先'] },
        toolbox: {
          show: true,
          orient: 'vertical',
          left: 'right',
          top: 'center',
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar', 'stack'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        xAxis: [{ type: 'category', axisTick: { show: false }, data: ['2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022', '2023', '2024',] }],
        yAxis: [{ type: 'value' }],
        series: [
          { name: '国内先进', type: 'bar', barGap: 0, label: labelOption, emphasis: { focus: 'series' }, data: [320, 332, 301, 334, 390,250,595,25,25,25,22,254,254] },
          { name: '国内领先', type: 'bar', label: labelOption, emphasis: { focus: 'series' }, data: [220, 182, 191, 234, 290,245,244,324,324,432,432,432,12] },
          { name: '国际先进', type: 'bar', label: labelOption, emphasis: { focus: 'series' }, data: [150, 232, 201, 154, 190,231,3,2,324,423,232,231,232] },
          { name: '国际领先', type: 'bar', label: labelOption, emphasis: { focus: 'series' }, data: [98, 77, 101, 99, 40,123,323,23,233,32,232,232,23] }
        ]
      };

      if (option && typeof option === 'object') {
        this.myChart.setOption(option);
      }

      app.config.onChange(); // 初始化时调用一次
    },
    resizeChart() {
      this.myChart.resize();
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
#chart-container {
  position: relative;
  height: 400px;
  overflow: hidden;
}
</style>
